package com.avic.map.service.impl;

import com.avic.map.common.model.ShipStatic24Model;
import com.avic.map.common.model.ShipStaticModel;
import com.avic.map.common.util.ex.BizErrorCode;
import com.avic.map.common.util.ex.BizException;
import com.avic.map.dal.model.ShipStaticInfo;
import com.avic.map.service.ShipStaticInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;


@Slf4j
@Service
public class ShipStaticInfoServiceImpl extends AbstractServiceImpl implements ShipStaticInfoService {

    @Override
    public ShipStaticInfo insertShipStaticByShipStatic24Model(ShipStatic24Model shipStaticModel) {
        try {
            ShipStaticInfo shipStaticInfo = new ShipStaticInfo();
            shipStaticInfo.setDataTime(shipStaticModel.getDateTime());
            DateTimeFormatter ftf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            shipStaticInfo.setDataTimeTxt(ftf.format(
                    LocalDateTime.ofInstant(Instant.ofEpochMilli(shipStaticModel.getDateTime()), ZoneId.systemDefault())));
            shipStaticInfo.setShipName(shipStaticModel.getShipName());
            shipStaticInfo.setShipNumber(String.valueOf(shipStaticModel.getMmsi()));
            shipStaticInfo.setCallsign(shipStaticModel.getCallsign());
            shipStaticInfo.setShipType(shipStaticModel.getShipType());
            shipStaticInfo.setShipLong(shipStaticModel.getToBow() + shipStaticModel.getToStern());
            shipStaticInfo.setShipWidth(shipStaticModel.getToPort() + shipStaticModel.getToStarboard());

            if (null != shipStaticInfoMapper.queryShipStaticInfo(shipStaticInfo.getShipNumber())) {
                log.info("更新船舶静态信息");
                shipStaticInfoMapper.updateShipStaticInfo(shipStaticInfo);
            } else {
                shipStaticInfoMapper.insertShipStatic(shipStaticInfo);
            }
            return shipStaticInfo;
        } catch (Exception e) {
            log.error("新增船舶静态信息发生错误 {} ", e.getMessage());
            throw new BizException(BizErrorCode.SYSTEM_ERROR);
        }
    }

    @Override
    public ShipStaticInfo insertShipStatic(ShipStaticModel shipStaticModel) {
        try {
            ShipStaticInfo shipStaticInfo = new ShipStaticInfo();
            shipStaticInfo.setCallsign(shipStaticModel.getCallsign());
            shipStaticInfo.setDestination(shipStaticModel.getDestination());
            shipStaticInfo.setDraught(shipStaticModel.getDraught());
            shipStaticInfo.setImo(shipStaticModel.getImo());
            shipStaticInfo.setShipType(shipStaticModel.getShipType());
            shipStaticInfo.setShipLong(shipStaticModel.getToBow() + shipStaticModel.getToStern());
            shipStaticInfo.setShipWidth(shipStaticModel.getToPort() + shipStaticModel.getToStarboard());
            shipStaticInfo.setDataTime(shipStaticModel.getDateTime());
            DateTimeFormatter ftf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            shipStaticInfo.setDataTimeTxt(ftf.format(
                    LocalDateTime.ofInstant(Instant.ofEpochMilli(shipStaticModel.getDateTime()), ZoneId.systemDefault())));
            shipStaticInfo.setShipName(shipStaticModel.getShipName());
            shipStaticInfo.setShipNumber(String.valueOf(shipStaticModel.getMmsi()));

            shipStaticInfoMapper.deleteShipStatic(shipStaticInfo.getShipNumber());
            shipStaticInfoMapper.insertShipStatic(shipStaticInfo);
            return shipStaticInfo;
        } catch (Exception e) {
            log.error("新增船舶静态信息发生错误 {} ", e.getMessage());
            throw new BizException(BizErrorCode.SYSTEM_ERROR);
        }
    }

    @Override
    public void insertShipStaticByShipStaticInfo(ShipStaticInfo shipStaticInfo) {
        try {
            if (null != shipStaticInfoMapper.queryShipStaticInfo(shipStaticInfo.getShipNumber())) {
                log.info("更新船舶静态信息");
                shipStaticInfoMapper.updateShipStaticInfo(shipStaticInfo);
            } else {
                shipStaticInfoMapper.insertShipStatic(shipStaticInfo);
            }
        } catch (Exception e) {
            log.error("新增船舶静态信息发生错误 {} ", e.getMessage());
            throw new BizException(BizErrorCode.SYSTEM_ERROR);
        }
    }

    @Override
    public void deleteShipStatic(String shipNumber) {
        try {
            shipStaticInfoMapper.deleteShipStatic(shipNumber);
        } catch (Exception e) {
            log.error("删除船舶静态信息发生错误 {}", e.getMessage());
            throw new BizException(BizErrorCode.SYSTEM_ERROR);
        }
    }

    @Override
    public ShipStaticInfo queryShipStaticInfo(String shipNumber) {
        return shipStaticInfoMapper.queryShipStaticInfo(shipNumber);
    }
}
